app.controller('mainCtrl', function ($scope, $http, $q, URLService, AuthorityService,localStorageService) {
    $scope.lang = SYSCONF.lang;

    $scope.i18n = LANG['LOCAL_' + $scope.lang.toUpperCase()];

    $scope.isLogging = false;

    $scope.errMsg = null;

    $scope.login = function () {

        var param = angular.copy($scope.info);
        //用户名、密码均为空
        if (Ut.strUtils.isEmpty(param.userName)&&Ut.strUtils.isEmpty(param.passWord)) {
            $scope.errMsg = I18N.TIP.t3;
            return;
        }
        //用户名为空
        if (Ut.strUtils.isEmpty(param.userName)) {
            $scope.errMsg = I18N.TIP.t4;
            return;
        }
        //密码为空
        if (Ut.strUtils.isEmpty(param.passWord)) {
            $scope.errMsg = I18N.TIP.t5;
            return;
        }

        $scope.errMsg = null;
        $scope.isLogging = true;

        var tokenUrl = URLService.getURL('token');
        //获取token
        $http.post(tokenUrl).success(function (res) {
            //第一次打开系统时，请求超时nginx将错误页面字符串返回给前台，确保用户管理后台服务已启动完成
            if (angular.isObject(res)) {
                var token = res.data;
                param.passWord = strEnc(param.passWord, token, token, token);
                var url = URLService.getURL('login');
                // var url = 'http://10.170.120.34:54363/service-center/getApps';
                $http.post(url, param).success(function (res) {
                    if (res.success) {
                        //登录后将用户角色信息写入cookie(cookie有4k大小限制，cookie中只保留用户信息)
                        AuthorityService.setRole(res.data);
                        window.location.href='servicecenter/index.html';
                    } else {
                        $scope.errMsg = res.errorMsg;
                        $scope.isLogging = false;
                    }
                }).error(function () {
                    $scope.errMsg = I18N.ERR_Msg.netError;
                    $scope.isLogging = false;
                });
            } else {
                $scope.errMsg = I18N.ERR_Msg.netError;
                $scope.isLogging = false;
            }
        }).error(function () {
            $scope.errMsg = I18N.ERR_Msg.netError;
            $scope.isLogging = false;
        });

    };


    $scope.keydownEvt = function (e) {
        if (e.keyCode === 13) {
            $scope.login();
        }
    }

});